import aiogram as aio
from aiogram.utils import executor

from models import User, session
from utils import string_to_datetime

bot = aio.Bot(token='5331018377:AAEIytqGh9SkfBzXnXjhb6KrvdKZ7I1reWk')
dp = aio.Dispatcher(bot)


@dp.message_handler(commands=['start'])
async def send_welcome(message):
    await message.answer("Hello, Send me your birthday in format: dd.mm.yyyy")


@dp.message_handler()
async def update_user(message):
    try:
        user = User(user_id=message.from_user.id, chat_id=message.chat.id, birthday=string_to_datetime(message.text))
        session.add(user)
        session.commit()
        await message.answer("Your birthday is saved. To update it write your birthday in format: dd.mm.yyyy again")
    except Exception as e:
        print(e)
        await message.answer("Something went wrong. Please, try again")


if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True)
